﻿using System;
using System.Windows.Forms;
using Insesoft.Entities;
using InseSoft.UIControllers;

namespace Insesoft.UI.Procesos
{
    public partial class OrdenForm : BaseForm
    {
        #region Constructors

        public OrdenForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Declarations

        private readonly OrdenController controller = new OrdenController();

        #endregion

        #region Event Handlers

        private void OrdenFormLoad(object sender, EventArgs e)
        {
            ConfigureToolBar(toolStrip, ToolBarButtons.Close);
            LoadGrid(true);
        }

        private void ToolStripItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch ((ToolBarButtons)e.ClickedItem.Tag)
            {
                case ToolBarButtons.Close:
                    Close();
                    break;
            }
        }

        private void ActivosCheckBoxCheckedChanged(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                LoadGrid(activosCheckBox.Checked);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void GridCellValidated(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                var orden = grid.Rows[e.RowIndex].DataBoundItem as Orden;
                if (orden != null)
                {
                    controller.Save(orden);
                    grid.InvalidateRow(e.RowIndex);
                }
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        #endregion

        #region Methods

        private void LoadGrid(bool soloActivos)
        {
            var ordenes = controller.GetByStatus(soloActivos);
            grid.DataSource = ordenes;
            ConfigureGrid(grid, "ClienteNombre");
            grid.SelectionMode = DataGridViewSelectionMode.CellSelect;

            // Visibility
            foreach (DataGridViewColumn col in grid.Columns)
            {
                switch (col.Name)
                {
                    case "Id":
                    case "CotizacionId":
                    case "NumPedido":
                    case "ClienteNombre":
                    case "FecAplicada":
                    case "FecFinal":
                    case "FecInicio":
                    case "NumDias":
                        col.Visible = true;
                        col.SortMode = DataGridViewColumnSortMode.Automatic;
                        break;
                    default:
                        col.Visible = false;
                        break;
                }
            }

            // Column width
            grid.Columns["Id"].Width = 60;
            grid.Columns["CotizacionId"].Width = 65;
            grid.Columns["NumPedido"].Width = 70;
            grid.Columns["ClienteNombre"].Width = 200;
            grid.Columns["FecAplicada"].Width = 90;
            grid.Columns["FecInicio"].Width = 90;
            grid.Columns["FecFinal"].Width = 90;
            grid.Columns["NumDias"].Width = 50;

            // Editable
            grid.ReadOnly = false;
            foreach (DataGridViewColumn col in grid.Columns)
                col.ReadOnly = true;
            grid.Columns["FecInicio"].ReadOnly = false;
            grid.Columns["FecFinal"].ReadOnly = false;

            // Column order
            grid.Columns["Id"].DisplayIndex = 0;
            grid.Columns["CotizacionId"].DisplayIndex = 1;
            grid.Columns["FecAplicada"].DisplayIndex = 2;
            grid.Columns["NumPedido"].DisplayIndex = 3;
            grid.Columns["ClienteNombre"].DisplayIndex = 4;
            grid.Columns["FecInicio"].DisplayIndex = 5;
            grid.Columns["FecFinal"].DisplayIndex = 6;
            grid.Columns["NumDias"].DisplayIndex = 7;

            // Headers
            grid.Columns["CotizacionId"].HeaderText = "Cotización";
            grid.Columns["FecAplicada"].HeaderText = "Aplicada";
            grid.Columns["NumPedido"].HeaderText = "Pedido";
            grid.Columns["ClienteNombre"].HeaderText = "Cliente";
            grid.Columns["FecInicio"].HeaderText = "Inicio";
            grid.Columns["FecFinal"].HeaderText = "Final";
            grid.Columns["NumDias"].HeaderText = "Días";
        }

        private void GridCellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (grid.Columns[e.ColumnIndex].Name == "FecInicio" || grid.Columns[e.ColumnIndex].Name == "FecFinal")
            {
                if (e.FormattedValue != null && !string.IsNullOrEmpty(e.FormattedValue.ToString()))
                {
                    var value = e.FormattedValue.ToString();
                    DateTime result;
                    e.Cancel = !DateTime.TryParse(value, out result);
                    if (e.Cancel)
                        MessageBox.Show(value + " no es una fecha válida", Text, MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                }
            }
        }

        #endregion
    }
}
